package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.ContainerForm;
import com.vcaresolution.logistic.pojo.Container;
import com.vcaresolution.logistic.pojo.Port;
import com.vcaresolution.logistic.pojo.Voyage;
import com.vcaresolution.logistic.to.ContainerTO;

@Service
public class ContainerServiceImpl extends BaseService implements ContainerService{

	private Container populateContainerPojo(ContainerForm jobregisterForm){
		Container container = new Container();
		BeanUtils.copyProperties(jobregisterForm, container);
		
		return container;
	}
	
	@Override
	@Transactional
	public void add(ContainerForm containerForm) {
		Container container = populateContainerPojo(containerForm);

		try{
			
			container.setPortByPodid(getDaoFactory().getUtilityDAO().findById(Port.class, containerForm.getPodid()));
			container.setPortByPolid(getDaoFactory().getUtilityDAO().findById(Port.class, containerForm.getPolid()));
			container.setPortByPfdid(getDaoFactory().getUtilityDAO().findById(Port.class, containerForm.getPfdid()));
			
			container.setVoyage(getDaoFactory().getUtilityDAO().findById(Voyage.class, containerForm.getOnboardvesselid()));
			
			getDaoFactory().getUtilityDAO().add(container);
		}
		catch(Exception e){
			e.printStackTrace();
			System.out.println("Exception occurred while adding");
		}
	}

	@Override
	@Transactional
	public void update(ContainerForm containerForm) {
		Container container = populateContainerPojo(containerForm);

		container.setPortByPodid(getDaoFactory().getUtilityDAO().findById(Port.class, containerForm.getPodid()));
		container.setPortByPolid(getDaoFactory().getUtilityDAO().findById(Port.class, containerForm.getPolid()));
		container.setPortByPfdid(getDaoFactory().getUtilityDAO().findById(Port.class, containerForm.getPfdid()));
		
		container.setVoyage(getDaoFactory().getUtilityDAO().findById(Voyage.class, containerForm.getOnboardvesselid()));
		
		getDaoFactory().getUtilityDAO().update(container);
	}

	@Override
	@Transactional
	public void delete(ContainerForm containerForm) {
		getDaoFactory().getUtilityDAO().delete(populateContainerPojo(containerForm));
	}

	@Override
	@Transactional
	public ContainerTO findById(Serializable containerID) {
		Container container = (Container) getDaoFactory().getUtilityDAO().findById(Container.class,containerID);
		
		ContainerTO containerTO = new ContainerTO();
		BeanUtils.copyProperties(container, containerTO);
		
		containerTO.setPodid(container.getPortByPodid().getPortid());
		containerTO.setPolid(container.getPortByPolid().getPortid());
		containerTO.setPfdid(container.getPortByPfdid().getPortid());
		
		containerTO.setOnboardvesselid(container.getVoyage().getVoyageid());
		containerTO.setUserid(container.getUser().getUserid());
		
		return containerTO; 
	}

	@Override
	@Transactional
	public List<ContainerTO> findByAll() {
		List<Container> containerList = getDaoFactory().getUtilityDAO().findByAll(Container.class);
		
		List<ContainerTO> containerTOList = new ArrayList<ContainerTO>();
		
		if( containerList != null && !containerList.isEmpty() ){
			ContainerTO containerTO = null;
			for(Container container: containerList){
				containerTO = new ContainerTO();
				BeanUtils.copyProperties(container, containerTO);
				
				containerTO.setPodid(container.getPortByPodid().getPortid());
				containerTO.setPolid(container.getPortByPolid().getPortid());
				containerTO.setPfdid(container.getPortByPfdid().getPortid());
				
				containerTO.setOnboardvesselid(container.getVoyage().getVoyageid());
				containerTO.setUserid(container.getUser().getUserid());
				
				containerTOList.add(containerTO);
			}
		}	
		return containerTOList;
	}

}